package panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;


public class CalculatorPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private	String input	= "";
	private String output	= "";
	private ExpressionPanel expressionPanel;
	/**
	 * Create the panel.
	 */
	public CalculatorPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel MainPanel = new JPanel();
		MainPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		add(MainPanel, BorderLayout.CENTER);
		MainPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel allButtonsPanel = new JPanel();
		allButtonsPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
//		allButtonsPanel.setLayout(
//			new FormLayout(new ColumnSpec[] {
//				ColumnSpec.decode("default:grow"),
//				ColumnSpec.decode("default:grow"),
//				ColumnSpec.decode("default:grow"),
//				ColumnSpec.decode("default:grow"),},
//			new RowSpec[] {
//				RowSpec.decode("default:grow(3)"),
//				RowSpec.decode("default:grow(1)"),
//				RowSpec.decode("default:grow"),
//				RowSpec.decode("default:grow"),
//				RowSpec.decode("default:grow"),
//				RowSpec.decode("default:grow"),
//				RowSpec.decode("default:grow"),}));
//		
		//
		// Output screen
		//
		
		expressionPanel = new ExpressionPanel();
		expressionPanel.setButtonVisible(false);
		//OutputPanel.add(expressionPanel);
		allButtonsPanel.add(expressionPanel,		"1, 1, 4, 1, fill, fill");
		
		//
		// Operators
		//
		
		JButton button_decimal		= new JButton(".");
		JButton button_0			= new JButton("0");
		JButton button_roll			= new JButton("ROLL");
		JButton button_multiply		= new JButton("*");
		JButton button_open			= new JButton("(");
		JButton button_close		= new JButton(")");
		JButton buttton_modulus		= new JButton("MOD");
		JButton button_subtract		= new JButton("-");
		JButton button_add			= new JButton("+");
		JButton button_divide		= new JButton("/");
		JButton button_power		= new JButton("POW");
		JButton button_equals		= new JButton("=");
		JButton buttonn_delete		= new JButton("DEL");
		JButton button_clear		= new JButton("CLR");

		button_decimal.addActionListener(	new ButtonListener(button_decimal));
		button_0.addActionListener(			new ButtonListener(button_0));
		button_roll.addActionListener(		new ButtonListener(button_roll));
		button_multiply.addActionListener(	new ButtonListener(button_multiply));
		button_open.addActionListener(		new ButtonListener(button_open));
		button_close.addActionListener(		new ButtonListener(button_close));
		buttton_modulus.addActionListener(	new ButtonListener(buttton_modulus));
		button_subtract.addActionListener(	new ButtonListener(button_subtract));
		button_add.addActionListener(		new ButtonListener(button_add));
		button_divide.addActionListener(	new ButtonListener(button_divide));
		button_power.addActionListener(		new ButtonListener(button_power));
		button_equals.addActionListener(	new EvaluateButtonListener());
		buttonn_delete.addActionListener(	new DeleteButtonListener());
		button_clear.addActionListener(		new ClearButtonListener());
		
		allButtonsPanel.add(buttton_modulus,	"3, 3, 1, 1, fill, fill");
		allButtonsPanel.add(button_clear, 		"3, 2, 1, 1, fill, fill");
		allButtonsPanel.add(button_roll,		"1, 2, 1, 1, fill, fill");
		allButtonsPanel.add(buttonn_delete, 	"4, 2, 1, 1, fill, fill");
		allButtonsPanel.add(button_open,		"1, 3, 1, 1, fill, fill");
		allButtonsPanel.add(button_close,		"2, 3, 1, 1, fill, fill");
		allButtonsPanel.add(button_subtract,	"4, 4, 1, 1, fill, fill");
		allButtonsPanel.add(button_add,			"4, 3, 1, 1, fill, fill");
		allButtonsPanel.add(button_multiply,	"4, 5, 1, 1, fill, fill");
		allButtonsPanel.add(button_divide,		"4, 6, 1, 1, fill, fill");
		allButtonsPanel.add(button_power,		"2, 2, 1, 1, fill, fill");
		allButtonsPanel.add(button_equals, 		"3, 7, 2, 1, fill, fill");
		
		//
		// Operands
		//
		
		JButton button_1 = new JButton("1");
		JButton button_2 = new JButton("2");
		JButton button_3 = new JButton("3");
		JButton button_4 = new JButton("4");
		JButton button_5 = new JButton("5");
		JButton button_6 = new JButton("6");
		JButton button_7 = new JButton("7");
		JButton button_8 = new JButton("8");
		JButton button_9 = new JButton("9");
		
		button_1.addActionListener(new ButtonListener(button_1));
		button_2.addActionListener(new ButtonListener(button_2));
		button_3.addActionListener(new ButtonListener(button_3));
		button_4.addActionListener(new ButtonListener(button_4));
		button_5.addActionListener(new ButtonListener(button_5));
		button_6.addActionListener(new ButtonListener(button_6));
		button_7.addActionListener(new ButtonListener(button_7));
		button_8.addActionListener(new ButtonListener(button_8));
		button_9.addActionListener(new ButtonListener(button_9));
		
		allButtonsPanel.add(button_decimal,		"1, 7, 1, 1, fill, fill");
		allButtonsPanel.add(button_0,			"2, 7, 1, 1, fill, fill");
		allButtonsPanel.add(button_1, 			"1, 6, 1, 1, fill, fill");
		allButtonsPanel.add(button_2, 			"2, 6, 1, 1, fill, fill");
		allButtonsPanel.add(button_3, 			"3, 6, 1, 1, fill, fill");
		allButtonsPanel.add(button_4, 			"1, 5, 1, 1, fill, fill");
		allButtonsPanel.add(button_5, 			"2, 5, 1, 1, fill, fill");
		allButtonsPanel.add(button_6, 			"3, 5, 1, 1, fill, fill");
		allButtonsPanel.add(button_7, 			"1, 4, 1, 1, fill, fill");
		allButtonsPanel.add(button_8, 			"2, 4, 1, 1, fill, fill");
		allButtonsPanel.add(button_9, 			"3, 4, 1, 1, fill, fill");

		
		MainPanel.add(allButtonsPanel);


		
	}

	
	private class ButtonListener implements ActionListener {

		private final JButton button;
		
		public ButtonListener(JButton button) {
			this.button = button;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			addText(button.getText());
		}
		
	}
	
	private class EvaluateButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			getExpressionPanel().evaluate();
		}
		
	}
	
	private class DeleteButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String	S = getExpressionPanel().getScreenText();
			if (S==null) S = "";
			if (S.length()>0) {
				S = S.substring(0, S.length()-1);
			}
			getExpressionPanel().getScreenTextField().setText(S);
		}
		
	}
	
	private class ClearButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			getExpressionPanel().getScreenTextField().setText("");
		}
		
	}
	
	//
	// Internal methods
	//
	
	private void addText(String text) {
		//
		// The one method will parse and text coming in
		//
		String	input = getInput().toUpperCase();
		if (text.equals("MOD"))							{	text = "%";	}
		if (text.equals("ROLL"))						{	text = "d";	}
		if (text.equals("POW"))							{	text = "^";	}
		
		setInput(input + text);
	}

	public String getInput() {
		return getExpressionPanel().getScreenTextField().getText();
	}

	public void setInput(String input) {
		getExpressionPanel().getScreenTextField().setText(input);
	}

	public String getOutput() {
		getExpressionPanel().evaluate();
		return getExpressionPanel().getScreenTextField().getText();
	}
	
	public ExpressionPanel getExpressionPanel() {
		return expressionPanel;
	}
}
